<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Looping/Iteration techniques</title>
	<link rel="stylesheet" type="text/css" href="../media/style.css">
	<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'/>
	<script src="media/a.js"></script>
</head>
<body>

<table border="0" cellspacing="0" cellpadding="0" height="48" width="100%">
  <tr>
	<td class="header-top-left"><img src="../media/logo.png" border="0" alt="phpDocumentor " /></td>
    <td class="header-top-right">QueryPath<br /><div class="header-top-right-subpackage"></div></td>
  </tr>
  <tr><td colspan="2" class="header-line"><img src="../media/empty.png" width="1" height="1" border="0" alt=""  /></td></tr>
  <tr>
    <td colspan="2" class="header-menu">
      
                        
                                                  		[ <a href="../classtrees_QueryPath.html" class="menu">class tree: QueryPath</a> ]
		[ <a href="../elementindex_QueryPath.html" class="menu">index: QueryPath</a> ]
            [ <a href="../elementindex.html" class="menu">all elements</a> ]
    </td>
  </tr>
  <tr><td colspan="2" class="header-line"><img src="../media/empty.png" width="1" height="1" border="0" alt=""  /></td></tr>
</table>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="195" class="menu">
		<div class="package-title">QueryPath</div>
  <div class="package">
	<div id="todolist">
			<p><a href="../todolist.html">Todo List</a></p>
	</div>
	</div>
      <b>Packages:</b><br />
  <div class="package">
              <a href="../li_QueryPath.html">QueryPath</a><br />
              <a href="../li_Examples.html">Examples</a><br />
              <a href="../li_Tests.html">Tests</a><br />
      	</div>
      <br />
                        <br />
                      </td>
    <td>
      <table cellpadding="10" cellspacing="0" width="100%" border="0"><tr><td valign="top">

<h1 align="center">Looping/Iteration techniques</h1>
<div class="src-code"><span class="php">
<ol><li><div class="src-line"><span class="src-php">&lt;?php</span></div></li>
<li><div class="src-line"><span class="src-doc">/**</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;This&nbsp;file&nbsp;shows&nbsp;five&nbsp;different&nbsp;ways&nbsp;to&nbsp;iterate&nbsp;through&nbsp;the&nbsp;contents&nbsp;of&nbsp;a</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;QueryPath.</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;QueryPath&nbsp;wraps&nbsp;zero&nbsp;or&nbsp;more&nbsp;elements&nbsp;(or,&nbsp;more&nbsp;generally,&nbsp;just&nbsp;objects).</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;Most&nbsp;of&nbsp;the&nbsp;operations&nbsp;available&nbsp;in&nbsp;QueryPath&nbsp;will&nbsp;operate&nbsp;on&nbsp;the&nbsp;entire</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;set&nbsp;of&nbsp;objects.&nbsp;Sometimes,&nbsp;though,&nbsp;it&nbsp;is&nbsp;desirable&nbsp;to&nbsp;go&nbsp;through&nbsp;the&nbsp;results</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;one&nbsp;at&nbsp;a&nbsp;time.&nbsp;These&nbsp;examples&nbsp;exhibit&nbsp;five&nbsp;ways&nbsp;of&nbsp;accomplishing&nbsp;this.</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;Keep&nbsp;in&nbsp;mind&nbsp;that&nbsp;PHP&nbsp;passes&nbsp;objects&nbsp;(more&nbsp;or&nbsp;less)&nbsp;by&nbsp;reference,&nbsp;which&nbsp;means</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;that&nbsp;a&nbsp;change&nbsp;to&nbsp;an&nbsp;item&nbsp;inside&nbsp;of&nbsp;a&nbsp;loop&nbsp;will&nbsp;be&nbsp;reflected&nbsp;in&nbsp;the&nbsp;main&nbsp;QueryPath.</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;In&nbsp;other&nbsp;words,&nbsp;there&nbsp;is&nbsp;no&nbsp;need&nbsp;to&nbsp;put&nbsp;data&nbsp;back&nbsp;into&nbsp;the&nbsp;QueryPath&nbsp;if&nbsp;it&nbsp;is</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;altered&nbsp;during&nbsp;a&nbsp;loop.</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@see</span><span class="src-doc">&nbsp;QueryPath</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@package</span><span class="src-doc">&nbsp;Examples</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@author</span><span class="src-doc">&nbsp;M&nbsp;Butcher&nbsp;&lt;matt@aleph-null.tv&gt;</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*&nbsp;</span><span class="src-doc-coretag">@license</span><span class="src-doc">&nbsp;LGPL&nbsp;(The&nbsp;GNU&nbsp;Lesser&nbsp;GPL)&nbsp;or&nbsp;an&nbsp;MIT-like&nbsp;license.</span></div></li>
<li><div class="src-line"><span class="src-doc">&nbsp;*/</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line">require&nbsp;<span class="src-str">'../src/QueryPath/QueryPath.php'</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-var">$demo&nbsp;</span>=&nbsp;<span class="src-str">'&lt;?xml&nbsp;version=&quot;1.0&quot;&nbsp;?&gt;</span></div></li>
<li><div class="src-line"><span class="src-str">&lt;data&gt;</span></div></li>
<li><div class="src-line"><span class="src-str">&lt;li&gt;Foo&lt;/li&gt;</span></div></li>
<li><div class="src-line"><span class="src-str">&lt;li&gt;Foo&lt;/li&gt;</span></div></li>
<li><div class="src-line"><span class="src-str">&lt;li&gt;Foo&lt;/li&gt;</span></div></li>
<li><div class="src-line"><span class="src-str">&lt;li&gt;Foo&lt;/li&gt;</span></div></li>
<li><div class="src-line"><span class="src-str">&lt;li&gt;Foo&lt;/li&gt;</span></div></li>
<li><div class="src-line"><span class="src-str">&lt;/data&gt;</span></div></li>
<li><div class="src-line"><span class="src-str">'</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-var">$qp&nbsp;</span>=&nbsp;<a href="../QueryPath/_src---QueryPath---QueryPath.php.html#functionqp">qp</a><span class="src-sym">(</span><span class="src-var">$demo</span><span class="src-sym">,&nbsp;</span><span class="src-str">'data'</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;Iterate&nbsp;over&nbsp;elements&nbsp;as&nbsp;DOMNodes:</span></div></li>
<li><div class="src-line"><span class="src-key">foreach&nbsp;</span><span class="src-sym">(</span><span class="src-var">$qp</span><span class="src-sym">-&gt;</span><span class="src-id">get</span><span class="src-sym">(</span><span class="src-sym">)&nbsp;</span><span class="src-key">as&nbsp;</span><span class="src-var">$li_ele</span><span class="src-sym">)&nbsp;</span><span class="src-sym">{</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;print&nbsp;<span class="src-var">$li</span><span class="src-sym">-&gt;</span><span class="src-id">tagName&nbsp;</span>.&nbsp;<span class="src-id">PHP_EOL</span><span class="src-sym">;&nbsp;</span><span class="src-comm">//&nbsp;Prints&nbsp;'li'&nbsp;five&nbsp;times.</span></div></li>
<li><div class="src-line"><span class="src-sym">}</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;Iterate&nbsp;over&nbsp;elements&nbsp;as&nbsp;QueryPath&nbsp;objects</span></div></li>
<li><div class="src-line"><span class="src-key">foreach&nbsp;</span><span class="src-sym">(</span><span class="src-var">$qp&nbsp;</span><span class="src-key">as&nbsp;</span><span class="src-var">$li_qp</span><span class="src-sym">)&nbsp;</span><span class="src-sym">{</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;print&nbsp;<span class="src-var">$li</span><span class="src-sym">-&gt;</span><span class="src-id">tag</span><span class="src-sym">(</span><span class="src-sym">)&nbsp;</span>.&nbsp;<span class="src-id">PHP_EOL</span><span class="src-sym">;&nbsp;</span><span class="src-comm">//&nbsp;Prints&nbsp;'li'&nbsp;five&nbsp;times</span></div></li>
<li><div class="src-line"><span class="src-sym">}</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-key">function&nbsp;</span><a href="../Examples/_examples---techniques.php.html#functioncallbackFunction">callbackFunction</a><span class="src-sym">(</span><span class="src-var">$index</span><span class="src-sym">,&nbsp;</span><span class="src-var">$element</span><span class="src-sym">)&nbsp;</span><span class="src-sym">{</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;print&nbsp;<span class="src-var">$element</span><span class="src-sym">-&gt;</span><span class="src-id">tagName&nbsp;</span>.&nbsp;<span class="src-id">PHP_EOL</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-sym">}</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;Iterate&nbsp;using&nbsp;a&nbsp;callback&nbsp;function</span></div></li>
<li><div class="src-line"><span class="src-var">$qp</span><span class="src-sym">-&gt;</span><a href="http://www.php.net/each">each</a><span class="src-sym">(</span><span class="src-str">'callbackFunction'</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;Iterate&nbsp;using&nbsp;a&nbsp;Lambda-style&nbsp;function</span></div></li>
<li><div class="src-line"><span class="src-var">$qp</span><span class="src-sym">-&gt;</span><span class="src-id">eachLambda</span><span class="src-sym">(</span><span class="src-str">'return&nbsp;$item-&gt;tagName&nbsp;.&nbsp;PHP_EOL;'</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;</div></li>
<li><div class="src-line"><span class="src-comm">//&nbsp;Loop&nbsp;through&nbsp;by&nbsp;index/count</span></div></li>
<li><div class="src-line"><span class="src-key">for&nbsp;</span><span class="src-sym">(</span><span class="src-var">$i&nbsp;</span>=&nbsp;<span class="src-num">0</span><span class="src-sym">;&nbsp;</span><span class="src-var">$i&nbsp;</span>&lt;&nbsp;<span class="src-var">$qp</span><span class="src-sym">-&gt;</span><span class="src-id">size</span><span class="src-sym">(</span><span class="src-sym">)</span><span class="src-sym">;&nbsp;</span>++<span class="src-var">$i</span><span class="src-sym">)&nbsp;</span><span class="src-sym">{</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;<span class="src-var">$domElement&nbsp;</span>=&nbsp;<span class="src-var">$qp</span><span class="src-sym">-&gt;</span><span class="src-id">get</span><span class="src-sym">(</span><span class="src-var">$i</span><span class="src-sym">)</span><span class="src-sym">;</span></div></li>
<li><div class="src-line">&nbsp;&nbsp;print&nbsp;<span class="src-var">$domElement</span><span class="src-sym">-&gt;</span><span class="src-id">tagName&nbsp;</span>.&nbsp;<span class="src-id">PHP_EOL</span><span class="src-sym">;</span></div></li>
<li><div class="src-line"><span class="src-sym">}</span></div></li>
</ol>
</span></div>
        <div class="credit">
		    <hr class="separator" />
		    Documentation generated on Thu, 19 Mar 2009 18:01:17 -0500 by <a href="http://www.phpdoc.org">phpDocumentor 1.4.2</a>
	      </div>
      </td></tr></table>
    </td>
  </tr>
</table>

</body>
</html>